package adc.app.ui.wicket.table;

import java.util.Arrays;
import java.util.List;

import org.apache.wicket.MarkupContainer;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.repeater.data.sort.AjaxFallbackOrderByBorder;
import org.apache.wicket.extensions.ajax.markup.html.repeater.data.table.AjaxFallbackHeadersToolbar;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.ISortStateLocator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.ISortableDataProvider;
import org.apache.wicket.extensions.markup.html.repeater.data.table.NoRecordsToolbar;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.OddEvenItem;
import org.apache.wicket.model.IModel;

public class DataTable<T> extends org.apache.wicket.extensions.markup.html.repeater.data.table.DataTable<T> {
	private static final long serialVersionUID = 1L;

	private NavigationToolbar nav;
	
	/**
	 * Constructor
	 * 
	 * @param id
	 *            component id
	 * @param columns
	 *            list of columns
	 * @param dataProvider
	 *            data provider
	 * @param rowsPerPage
	 *            number of rows per page
	 */
	@SuppressWarnings("unchecked")
	public DataTable(String id, final List<IColumn<T>> columns,
			ISortableDataProvider<T> dataProvider, int rowsPerPage) {
		this(id, columns.toArray((IColumn<T>[]) new IColumn[columns.size()]),
				dataProvider, rowsPerPage);
	}

	/**
	 * Constructor
	 * 
	 * @param id
	 *            component id
	 * @param columns
	 *            array of columns
	 * @param dataProvider
	 *            data provider
	 * @param rowsPerPage
	 *            number of rows per page
	 */
	public DataTable(String id, final IColumn<T>[] columns,
			ISortableDataProvider<T> dataProvider, int rowsPerPage) {
		super(id, Arrays.asList(columns), dataProvider, rowsPerPage);
		setOutputMarkupId(true);
		setVersioned(false);
		addTopToolbar(new AjaxFallbackHeadersToolbar(this, dataProvider) {
			private static final long serialVersionUID = 1L;

			@Override
			protected WebMarkupContainer newSortableHeader(String borderId,
					String property, ISortStateLocator locator) {
				return new AjaxFallbackOrderByBorder(borderId, property, locator, getAjaxCallDecorator()) {
					private static final long serialVersionUID = 1L;

					@Override
					protected void onAjaxClick(AjaxRequestTarget target) {
						target.add(getTable());
					}

				};
			}
		});
		addBottomToolbar(new NoRecordsToolbar(this));
		addBottomToolbar(nav = new NavigationToolbar(this));
	}
	
	

	/**
	 * Add a link to the navigation toolbar.
	 * @param link link to add
	 * @return the table
	 */
	public DataTable<T> addLink(MarkupContainer link) {
		nav.addLink(link);
		return this;
	}

	/**
	 * Returns the id that link components added to the navigation toolbar must have.
	 * @return the id that link components added to the navigation toolbar must have
	 */
	public String getLinkId() {
		return nav.getLinkId();
	}

	/**
	 * Returns the id that labels inside link components added to the navigation toolbar must have.
	 * @return the id that labels inside link components added to the navigation toolbar must have
	 */
	public String getLinkLabelId() {
		return nav.getLinkLabelId();
	}

	/**
	 * Create new row item.
	 */
	@Override
	protected Item<T> newRowItem(String id, int index, IModel<T> model) {
		return new OddEvenItem<T>(id, index, model);
	}
}
